package com.cloudkinto.service.dic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cloudkinto.common.utils.StringsUtil;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.entity.DicBusinessItemDo;
import com.cloudkinto.dao.DicBusinessItemDao;
import com.cloudkinto.service.dic.DicBusinessItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.service.dic.vo.DicItemRes;
import com.cloudkinto.service.dic.vo.dicBusinessItem.DicBusinessItemAddReq;
import com.cloudkinto.service.dic.vo.dicBusinessItem.DicBusinessItemDetailRes;
import com.cloudkinto.service.dic.vo.dicBusinessItem.DicBusinessItemListPageRes;
import com.cloudkinto.service.dic.vo.dicBusinessItem.DicBusinessItemUpdateReq;
import com.cloudkinto.service.orderback.vo.FbaOrderBackProductAddReq;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.*;

/**
 * <p>
 * 码表明细表 service实现类
 * </p>
 * @author 张永远
 * @since 2020-05-19
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class DicBusinessItemServiceImpl extends ServiceImpl<DicBusinessItemDao, DicBusinessItemDo> implements DicBusinessItemService {
    @Autowired
    private DicBusinessItemDao dao;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public DicBusinessItemAddReq add(DicBusinessItemAddReq req, Long userId) {
        DicBusinessItemDo entityDo = new DicBusinessItemDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    //新增校验
    private void addCheck(DicBusinessItemAddReq req) {
    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(DicBusinessItemDo entityDo, Long userId) {
    }

    //将新增的do转换成要返回的实体
    private DicBusinessItemAddReq handlerAddAfter(DicBusinessItemDo entityDo) {
        DicBusinessItemAddReq res = new DicBusinessItemAddReq();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success(detail(id));
    }

    @Override
    public DicBusinessItemUpdateReq update(DicBusinessItemUpdateReq req, Long userId) {
        DicBusinessItemDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(DicBusinessItemDo entityDo, Long userId) {
    }

    //做修改之后要执行的业务定制处理
    private DicBusinessItemUpdateReq handlerUpdateAfter(DicBusinessItemDo entityDo) {
        DicBusinessItemUpdateReq res = new DicBusinessItemUpdateReq();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        DicBusinessItemDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(DicBusinessItemDo entityDo, Long userId) {
    }

    @Override
    public DicBusinessItemDetailRes detail(Long id) {
        DicBusinessItemDo entityDo =  dao.selectById(id);
        DicBusinessItemDetailRes res = new DicBusinessItemDetailRes();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private DicBusinessItemDetailRes handlerDetailAfter(DicBusinessItemDetailRes res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<DicBusinessItemListPageRes> getList(Map<String, String> map) {
        QueryWrapper<DicBusinessItemDo> wrapper = queryBuild(map);
        List<DicBusinessItemDo> list = this.list(wrapper);
        List<DicBusinessItemListPageRes> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<DicBusinessItemDo> wrapper = queryBuild(map);
        IPage<DicBusinessItemDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<DicBusinessItemListPageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int)page.getCurrent(), (int)page.getSize(), (int)page.getTotal(), (int)page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<DicBusinessItemListPageRes> handlerListAfter(List<DicBusinessItemDo> list ){
        List<DicBusinessItemListPageRes> result = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            for (DicBusinessItemDo businessItemDo : list) {
                DicBusinessItemListPageRes res = new DicBusinessItemListPageRes();
                BeanUtils.copyProperties(businessItemDo,res);
                result.add(res);
            }
        }
        return result;
    }

    private IPage<DicBusinessItemDo> pageInit(Map<String, String> map) {
        IPage<DicBusinessItemDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<DicBusinessItemDo> queryBuild(Map<String, String> map) {
        QueryWrapper<DicBusinessItemDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }

    @Override
    public List<DicItemRes> getDicItemList(Long dicBusinessId) {
        List<DicItemRes> trackList = this.getDicItemList(dicBusinessId, false);
        return trackList;
    }


    @Override
    public List<DicItemRes> getDicItemList(Long dicBusinessId, boolean sort) {
        LambdaQueryWrapper<DicBusinessItemDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DicBusinessItemDo::getDicBusinessId, dicBusinessId);
        if (sort) {
            wrapper.orderByAsc(DicBusinessItemDo::getSort);
        }
        List<DicBusinessItemDo> trackCompanyList = dao.selectList(wrapper);
        List<DicItemRes> trackList = new ArrayList<>();
        for (DicBusinessItemDo itemDo : trackCompanyList) {
            trackList.add(new DicItemRes(itemDo.getId(), StringsUtil.createI18Message(itemDo.getDicItemValue()), itemDo.getParentId()));
        }
        return trackList;
    }



    @Override
    public String getDicItemValue(Long dicBusinessItemId) {
        DicBusinessItemDo et = dao.selectById(dicBusinessItemId);
        if (et == null) {
            return "";
        }
        return StringsUtil.createI18Message(et.getDicItemValue());
    }
}
